

/*---------------------------------------------------------

  GMPB

  Paintball :D

---------------------------------------------------------*/


// These files get sent to the client

AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
AddCSLuaFile( "cl_spawnmenu.lua" )
AddCSLuaFile( "cl_notice.lua" )
AddCSLuaFile( "cl_hints.lua" )
AddCSLuaFile( "cl_tooltips.lua" )
AddCSLuaFile( "cl_worldtips.lua" )
AddCSLuaFile( "player_extension.lua" )
AddCSLuaFile( "cl_scoreboard.lua" )
AddCSLuaFile( "player.lua" )
AddCSLuaFile( "scoreboard/admin_buttons.lua" )
AddCSLuaFile( "scoreboard/player_frame.lua" )
AddCSLuaFile( "scoreboard/player_infocard.lua" )
AddCSLuaFile( "scoreboard/player_row.lua" )
AddCSLuaFile( "scoreboard/scoreboard.lua" )
AddCSLuaFile( "scoreboard/vote_button.lua" )
AddCSLuaFile( "gmpb_weapons.lua" )

include( 'shared.lua' )
include( 'commands.lua' )
include( 'player.lua' )
include( 'rating.lua' )
include( 'gmpb_weapons.lua')

GM.redFlag = nil
GM.redBase = nil
GM.bluFlag = nil
GM.bluBase = nil

GM.bluScore = 0
GM.redScore = 0

SetGlobalBool("RESET",false)
//SetGlobalBool("Announce",true)

local function ReloadToolMenu( ply )

	ply:ConCommand( "spawnmenu_reloadtools" )

end

function GM:Announce(team,str)
	for _, p in pairs(player.GetAll()) do
		p:PrintMessage(HUD_PRINTCENTER,((team == TEAM_BLUE) and "Blue" or "Red") .. " " .. str)
	end
end

function GM:AnnounceWinner(str)
	for _, p in pairs(player.GetAll()) do
		p:PrintMessage(HUD_PRINTCENTER, str)
	end
end

function GM:FlagCapped(team,ply)
	ply:AddFrags(5)
	if team == TEAM_BLUE then
		self.bluScore = self.bluScore + 1	
	elseif team == TEAM_RED then
		self.redScore = self.redScore + 1
	end
	SetGlobalInt("BlueScore",self.bluScore)
	SetGlobalInt("RedScore",self.redScore)
	SetGlobalBool((team == TEAM_RED and "Blue" or "Red") .. "Taken",false,true)
	ply:SetNetworkedBool("HasFlag",false,true)
	self:Announce(team, "Team Scores - Round Resetting...")
	//ply:ConCommand("+attack")
	//self:ResetTeams()
	SetGlobalBool("RESET",true)
	timer.Simple(2, GAMEMODE.StealWeapons, GAMEMODE)
	timer.Simple(3, GAMEMODE.ResetTeams, GAMEMODE)
	timer.Simple(4.0, GAMEMODE.ForceSpawn, GAMEMODE)
	timer.Simple(4.5, GAMEMODE.LockTeams, GAMEMODE)
     	timer.Simple(7, GAMEMODE.UnlockTeams, GAMEMODE)
end

function GM:FlagTaken(team,ply)
	self:Announce(team, "flag taken")
	SetGlobalBool((team == TEAM_BLUE and "Blue" or "Red") .. "Taken",true,true)
	ply:SetNetworkedBool("HasFlag",true,true)
	ply:AddFrags(1)
end

function GM:FlagDropped(team,ply)
	self:Announce(team, "flag dropped")
	SetGlobalBool((team == TEAM_BLUE and "Blue" or "Red") .. "Taken",false,true)
	ply:SetNetworkedBool("HasFlag",false,true)
end

function GM:FlagReturned(team,ply)
	//if GetGlobalBool("Announce") == false then
		self:Announce(team,"flag returned")
	//end
	//SetGlobalBool("Announce",true)
	SetGlobalBool((team == TEAM_BLUE and "Blue" or "Red") .. "Taken",false,true)
	ply:AddFrags(1)
end

function GM:ResetTeams()
  //SetGlobalBool("RESET",true)
      for k, v in pairs(player.GetAll()) do  
	v:ShouldDropWeapon(false) 
	v:KillSilent()
	
	local entities = ents.FindInSphere(v:GetPos(), 20)
	for _, we in pairs(entities) do
 		if(we:IsWeapon()) then
  			we:Remove()
 		end
	end 

	if v:Team() == (TEAM_SPECBLUE) then
     		v:SetTeam(TEAM_BLUE)
	end
        if v:Team() == (TEAM_SPECRED) then
		v:SetTeam(TEAM_RED)
        end	
      end
      
      //timer.Simple(3, UnlockTeams)
end	

function GM:StealWeapons()
	for k, v in pairs(player.GetAll()) do
		Msg("Steal weapons was Reached\n")
		v:StripWeapons()
	end 
end

function GM:ForceSpawn()
	for k, v in pairs(player.GetAll()) do
		Msg("Force Respawn was Reached\n")
		v:Spawn()
		//ply:Spawn()
	end 
end

function GM:LockTeams()
	for k, v in pairs(player.GetAll()) do
		Msg("Lock was Reached\n")
		//v:ConCommand("-attack")
		v:Lock()
		//v:Freeze(true)
	end 
end

function GM:UnlockTeams()
	for k, v in pairs(player.GetAll()) do
		Msg("Unlock was Reached\n")
		v:UnLock()
		//v:ConCommand("-attack")
		//v:Freeze(false)
		SetGlobalBool("RESET",false)
	end 
end


function GM:Initialize( )
	self:SetupTeams()
	self.BaseClass:Initialize()
	util.PrecacheSound("npc/roller/remote_yes.wav")
	util.PrecacheSound("ambient/alarms/klaxon1.wav")
	SetGlobalInt("BlueScore",0)
	SetGlobalInt("RedScore",0)
	SetGlobalBool("BlueTaken",false)
	SetGlobalBool("RedTaken",false)
end

function GM:InitPostEntity()
	-- create flag bases and flags
	local targs = ents.FindByClass("info_target") -- the game needs two info_targets named "redflag" and "blueflag"; everything else is automatic
	for _,t in pairs(targs) do
		if t:GetName() == "redflag" && self.redFlag == nil then
			self.redFlag = ents.Create("flag")
			self.redFlag:SetPos(t:GetPos()+Vector(0,0,36))
			self.redFlag:Spawn()
			self.redFlag:Setupify(TEAM_RED,self)
			
			self.redBase = ents.Create("flagbase")
			self.redBase:SetPos(t:GetPos()+Vector(0,0,-12))
			self.redBase:Spawn()
			self.redBase:Setupify(TEAM_RED,self.redFlag,self)
			
			self.redFlag:SetBase(self.redBase)
		end
		if t:GetName() == "blueflag" && self.bluFlag == nil then
			self.bluFlag = ents.Create("flag")
			self.bluFlag:SetPos(t:GetPos()+Vector(0,0,36))
			self.bluFlag:Spawn()
			self.bluFlag:Setupify(TEAM_BLUE,self)
			
			self.bluBase = ents.Create("flagbase")
			self.bluBase:SetPos(t:GetPos()+Vector(0,0,-12))
			self.bluBase:Spawn()
			self.bluBase:Setupify(TEAM_BLUE,self.bluFlag,self)
			
			self.bluFlag:SetBase(self.bluBase)
		end
	end
end

function GM:PlayerShouldTakeDamage(victim,attacker)
	return (victim:Team() != attacker:Team() || victim == attacker)
end

//function GM:PlayerDeath(victim, weapon, killer)
//	self.BaseClass:PlayerDeath(victim,weapon,killer)
//	if victim:GetVar("hasflag") != nil then
//		killer:AddFrags(1)
//	end
//	self:DropFlag(victim)
//	
//end

function GM:PlayerDeath(ply)
	self.BaseClass:PlayerSpawn(ply)
	ply:SetNetworkedBool("HasFlag",false,true)
	self:DropFlag(ply)
	if ply:Team() == TEAM_BLUE then
		Msg("Blue Player Joined BlueSpec \n")
		ply:SetTeam(TEAM_SPECBLUE)
	end
	if ply:Team() == TEAM_RED then
		Msg("Red Player Joined RedSpec \n")
		ply:SetTeam(TEAM_SPECRED)
	end
	
	//if team.NumPlayers(TEAM_BLUE) == 0 then
	//	for _, p in pairs(player.GetAll()) do
	//		p:PrintMessage("Red Team Wins - Round Resetting...")
	//	end	
	//end

	//if team.NumPlayers(TEAM_RED) == 0 then
	//	for _, p in pairs(player.GetAll()) do
	//		p:PrintMessage("Blue Team Wins - Round Resetting...")
	//	end	
	//end

	if team.NumPlayers(TEAM_BLUE) == 0 || team.NumPlayers(TEAM_RED) == 0 then
		if GetGlobalBool("RESET") == false then
			SetGlobalBool("RESET",true)
			
			if team.NumPlayers(TEAM_BLUE) == 0 then
				self:AnnounceWinner("Blue Team Eliminated - Round Resetting...")
			end
			if team.NumPlayers(TEAM_RED) == 0 then
				self:AnnounceWinner("Red Team Eliminated - Round Resetting...")
			end
			timer.Simple(2, GAMEMODE.StealWeapons, GAMEMODE)
			timer.Simple(3, GAMEMODE.ResetTeams, GAMEMODE)
			timer.Simple(4.0, GAMEMODE.ForceSpawn, GAMEMODE)
			timer.Simple(4.5, GAMEMODE.LockTeams, GAMEMODE)
     			timer.Simple(7, GAMEMODE.UnlockTeams, GAMEMODE)
		end
	end
			
end

function GM:PlayerDisconnected(ply)
	self:DropFlag(ply)
end

function GM:DropFlag(pl)
	local flg = pl:GetVar("hasflag")
	if flg != nil then
		flg:Drop(pl)
	end
	
end

function GM:Think( )
end

function GM:ShutDown( )
end

function GM:PlayerSetModel( pl )
	local models = {}
	if pl:GetVar("setmodel") == nil then
		if pl:Team() == TEAM_BLUE then
			//{"police","combine_soldier","combine_soldier_prisonguard","combine_super_soldier","breen"}
			models = {"combine_soldier_prisonguard"}
		end
		if pl:Team() == TEAM_RED then
			//{"alyx","barney","eli","kleiner","monk","mossman","odessa"}
			models = {"combine_soldier"}
		end
		if pl:Team() == TEAM_SPECRED || pl:Team() == TEAM_SPECBLUE then
			//{"alyx","barney","eli","kleiner","monk","mossman","odessa"}
			models = {"kleiner"}
		end
		
		local modelname = "models/player/" .. models[math.random(1,#models)] .. ".mdl"
		util.PrecacheModel( modelname )
		pl:SetModel( modelname )
		pl:SetVar("setmodel",modelname)
	else
		util.PrecacheModel(pl:GetVar("setmodel"))
		pl:SetModel(pl:GetVar("setmodel"))
	end
end

function GM:PlayerSpawn( pl )
	self.BaseClass:PlayerSpawn(pl)
	pl:SetNetworkedBool("HasFlag",false,true)
	pl:SetHealth(100)
end

function GM:PlayerInitialSpawn(pl)
	Msg("Blue: "..team.NumPlayers(TEAM_BLUE).."\nRed: " .. team.NumPlayers(TEAM_RED) .. "\n")
	
	local RedTotal = team.NumPlayers(TEAM_RED) + team.NumPlayers(TEAM_SPECRED)
	local BlueTotal = team.NumPlayers(TEAM_BLUE) + team.NumPlayers(TEAM_SPECBLUE)
	
	if BlueTotal > RedTotal + 1 then
		pl:SetTeam(TEAM_RED)
	else
		pl:SetTeam(TEAM_BLUE)
	end
end

function GM:PlayerSelectSpawn(pl)
	if self.redSpawns == nil then
		self.redSpawns = table.Add(self.redSpawns,ents.FindByClass("info_player_terrorist"))
	end
	if self.blueSpawns == nil then
		self.blueSpawns = table.Add(self.blueSpawns,ents.FindByClass("info_player_counterterrorist"))
	end
	if self.specblueSpawns == nil then
		self.specblueSpawns = table.Add(self.specblueSpawns,ents.FindByClass("info_player_start"))
	end
	if self.specredSpawns == nil then
		self.specredSpawns = table.Add(self.redspecSpawns,ents.FindByClass("info_player_start"))
	end
	
	local numSpwn = 0
	local cSpawnList = nil
	if pl:Team() == TEAM_BLUE then
		cSpawnList = self.blueSpawns
	end
	if pl:Team() == TEAM_SPECBLUE then
		cSpawnList = self.specblueSpawns
	end
	if pl:Team() == TEAM_RED then
		cSpawnList = self.redSpawns
	end
	if pl:Team() == TEAM_SPECRED then
		cSpawnList = self.specredSpawns
	end
	
	numSpwn = table.Count(cSpawnList)
	if numSpwn == 0 then
		Msg("PlayerSelectSpawn error: no spawn available\n")
		return nil
	end
	
	local ChosenSpawnPoint = nil
	
	for i=0,6 do
		ChosenSpawnPoint = cSpawnList[math.random(1,numSpwn)]
		if ChosenSpawnPoint && ChosenSpawnPoint:IsValid() && ChosenSpawnPoint:IsInWorld() && ChosenSpawnPoint != pl:GetVar( "LastSpawnpoint" ) then	
			pl:SetVar( "LastSpawnpoint", ChosenSpawnPoint )
			return ChosenSpawnPoint
		end
	end
	return ChosenSpawnPoint
end

function GM:PlayerLoadout( pl )

	if ( server_settings.Bool( "sbox_weapons", true ) ) then
		
	end

	if pl:Team() == TEAM_BLUE || pl:Team() == TEAM_RED then
		pl:Give("pb_dm4")
        pl:Give("pb_angel")
		pl:Give("pb_sniper")
		pl:Give("pb_a51")
		pl:Give("pb_cocker")
		pl:Give("pb_bush")
		pl:Give("pb_blazer")
	end
	
	local cl_defaultweapon = pl:GetInfo( "cl_defaultweapon" )
	if ( pl:HasWeapon( cl_defaultweapon )  ) then
		pl:SelectWeapon( cl_defaultweapon ) 
	end
	
end

function GM:ShutDown( )
end

hook.Add( "PlayerInitialSpawn", "PlayerReloadToolmenu", ReloadToolMenu )
hook.Add( "FlagCapped", "CapturedFlag", ResetTeams )


